import socket
import binascii

# 构建报文函数
def build_message():
    # 定义字段数据
    length_string = "256"          # 电文长度
    tele_no = 'AGDS01'            # 电文号
    mat_no = '1234567890123456'   # 钢卷号
    start_time_1 = '20240101120000'  # 1#机器人打磨 起始时间
    end_time_1 = '20240101180000'    # 1#机器人打磨 结束时间
    track_model_1 = '1'                # 轨迹模式 (1: 0°)
    down_force_1 = 123.45              # 1#机器人下压力
    start_time_2 = '2024010114:00'  # 2#机器人打磨 起始时间
    end_time_2 = '2024010114:00'    # 2#机器人打磨 结束时间
    track_model_2 = '2'                # 轨迹模式 (2: 45°)
    down_force_2 = 235.57              # 2#机器人下压力
    surface_flag = '1'                 # 上下表面标记 (1)

    # 转换数据为字节
    length_bytes = length_string.ljust(4, b'\x00').encode('ascii')  # 电文长度填充空格，确保4字节
    tele_no_bytes = tele_no.ljust(6, b'\x00').encode('ascii')  # 电文号填充空格，确保6字节
    mat_no_bytes = mat_no.ljust(20, b'\x00').encode('ascii')  # 钢卷号填充空格，确保20字节
    start_time_1_bytes = start_time_1.ljust(14,b'\x00').encode('ascii')  # 起始时间，确保14字节
    end_time_1_bytes = end_time_1.ljust(14, b'\x00').encode('ascii')  # 结束时间，确保14字节
    track_model_1_bytes = track_model_1.ljust(2, b'\x00').encode('ascii')  # 轨迹模式，确保2字节

    # 将浮动小数转为整数并填充为6个字节
    down_force_1_int = int(down_force_1 * 1000)  # 去掉小数点并转为整数
    down_force_1_bytes = f"{down_force_1_int:06d}".encode('ascii')  # 转换为6字节整数

    start_time_2_bytes = start_time_2.ljust(14, b'\x00').encode('ascii')  # 2#机器人的起始时间，确保14字节
    end_time_2_bytes = end_time_2.ljust(14, b'\x00').encode('ascii')  # 2#机器人的结束时间，确保14字节
    track_model_2_bytes = track_model_2.ljust(2, b'\x00').encode('ascii')  # 2#轨迹模式，确保2字节

    # 将浮动小数转为整数并填充为6个字节
    down_force_2_int = int(down_force_2 * 1000)  # 去掉小数点并转为整数
    down_force_2_bytes = f"{down_force_2_int:06d}".encode('ascii')  # 转换为6字节整数

    surface_flag_bytes = surface_flag.ljust(1, b'\x00').encode('ascii')  # 上下表面标记，确保1字节

    # 拼接所有字段为最终报文
    message = (length_bytes + tele_no_bytes + mat_no_bytes + 
               start_time_1_bytes + end_time_1_bytes + track_model_1_bytes +
               down_force_1_bytes + start_time_2_bytes + end_time_2_bytes +
               track_model_2_bytes + down_force_2_bytes + surface_flag_bytes)

    # 确保报文长度为 256 字节，不足时填充空格
    message = message.ljust(256, b'\x00')  # 其他部分填充空格，确保总长度为256字节

    # 打印报文的十六进制表示
    print(f"Sent (Hex): {binascii.hexlify(message).decode('ascii')}")
    return message

# 发送报文
def send_message():
    # 创建 TCP socket
    s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
    s.connect(('127.0.0.1', 5001))  # 连接到服务器（IP: 127.0.0.1, Port: 5001）

    # 构建报文
    message = build_message()

    # 发送报文
    s.sendall(message)
    print(f"Sent: {message}")

    # 接收响应
    data = s.recv(1024)
    print(f"Received: {data.decode('utf-8')}")

    # 关闭连接
    s.close()

# 执行发送报文
#send_message()
build_message()

323536004147445000313132333435363738390031323334353600000000320032340031003131323a000000320032340031003131323a00000031313233343500320032340031003131343a000000320032340031003131343a00000032323335353700310000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000